package com.wdd.myplatform.controller;

import com.wdd.myplatform.service.QRCodeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import javax.net.ssl.HttpsURLConnection;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;

/**
 * 生成二维码demo
 * @author Administrator
 */
@RestController
@RequestMapping("/QRCodeDemoController")
public class QRCodeDemoController {

    @RequestMapping("/getQRCode")
    public void getQRCode(String code , HttpServletResponse response , Map<String,Object> map){
        try {
            // 设置响应流信息
            response.setContentType("image/jpg");
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            OutputStream stream = response.getOutputStream();
            //获取一个二维码图片
            BufferedImage bi = new QRCodeService().GetQRCode(code);//调用层
            //以流的形式输出到前端
            ImageIO.write(bi, "jpg", stream);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 图片去除水印
     */
    @GetMapping("/removeWaterMark")
    @ApiOperation(value="图片去除水印", notes="图片去除水印")
    public void removeWaterMark(){
        File dirFile  = new File("F:\\test1.png");
        File saveDirFile = new File("F:\\test2.png");
        String dir = dirFile.getAbsolutePath();
        String saveDir = saveDirFile.getAbsolutePath();
        replaceColor(dir,saveDir);
    }

    private static void replaceColor(String srcFile, String dstFile) {
        try {
            Color color = new Color(255, 195, 195);
            replaceImageColor(srcFile, dstFile, color, Color.WHITE);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void replaceImageColor(String file, String dstFile, Color srcColor, Color targetColor) throws IOException {
        URL http;
        if (file.trim().startsWith("https")) {
            http = new URL(file);
            HttpsURLConnection conn = (HttpsURLConnection) http.openConnection();
            conn.setRequestMethod("GET");
        } else if (file.trim().startsWith("http")) {
            http = new URL(file);
            HttpURLConnection conn = (HttpURLConnection) http.openConnection();
            conn.setRequestMethod("GET");
        } else {
            http = new File(file).toURI().toURL();
        }
        BufferedImage bi = ImageIO.read(http.openStream());
        if(bi == null){
            return;
        }

//        Color wColor = new Color(238, 243, 249);//浅灰色
        for (int i = 0; i < bi.getWidth(); i++) {
            for (int j = 0; j < bi.getHeight(); j++) {
                int color = bi.getRGB(i, j);
                Color oriColor = new Color(color);
                int red = oriColor.getRed();
                int greed = oriColor.getGreen();
                int blue = oriColor.getBlue();
                if (red == 255 && greed == 255 && blue == 255) {
                    continue;
                }
                //去掉粉色水印(粉色替换为白色)
//                     if (red == 255 && greed > 180 && blue > 180) {
//                         bi.setRGB(i, j, wColor.getRGB());
//                     }
                //去掉灰色水印（灰色替换为白色）
//                 if (red == 229 && greed == 229 && blue == 229) {
//                         bi.setRGB(i, j, wColor.getRGB());
//                     }
                //去掉浅灰色水印（灰色替换为白色或替换为浅灰色）
                if (red >170 && greed > 170 && blue > 170) {
                    bi.setRGB(i, j, targetColor.getRGB());
                }
                if (red > 220 && greed > 180 && blue > 80) {
                    bi.setRGB(i, j, targetColor.getRGB());
                }
                if (red <= 240 && greed >= 200 && blue >= 150) {
                    bi.setRGB(i, j, targetColor.getRGB());
                }
            }
        }
        String type = file.substring(file.lastIndexOf(".") + 1, file.length());
        Iterator<ImageWriter> it = ImageIO.getImageWritersByFormatName(type);
        ImageWriter writer = it.next();
        File f = new File(dstFile);
        f.getParentFile().mkdirs();
        ImageOutputStream ios = ImageIO.createImageOutputStream(f);
        writer.setOutput(ios);
        writer.write(bi);
        bi.flush();
        ios.flush();
        ios.close();
    }

}
